#include <iostream>
#include "commad.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
using namespace std;

int main()
{
    umask(0);
    int n = mkfifo(path_name.c_str(), mode);
    if(n == -1)
    {
        cout << errno << ":" << strerror(errno) << endl;
        return 1;
    }

    int fd = open(path_name.c_str(), O_RDONLY);
    if(fd == -1)
    {
        cout << errno << ":" << strerror(errno) << endl;
        return 1;
    }
    cout << "I am ready for IPC!" << endl;

    char buff[NUM];
    while(true)
    {
        buff[0] = 0;
        ssize_t n = read(fd, buff, sizeof(buff) - 1);
        if(n > 0)
        {
            buff[n] = 0;
            cout << "I get it :" << buff << endl;
        }else if(n == 0)
        {
            cout << "There is nothing to be got" << endl;
            break;
        }
        else
        {
            cout << errno << ":" << strerror(errno) << endl;
            return 1;
        } 
    }

    close(fd);
    unlink(path_name.c_str());
    return 0;
}